﻿// CSVLiner.cpp: 定义应用程序的入口点。
//
#include <qfilesystemwatcher.h>
#include <qdebug.h>
#include <qfile.h>

#include "CsvPlot.h"
#include <qwt/qwt.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qapplication.h>

#include <QColor>
using namespace std;
using namespace csv;

struct labelID {
	int row = -1, cloumn = -1;
};

namespace csv {
	CsvReader::CsvReader(QString fn, QString speator, unsigned int skip_rows, int labelIdx) :
		mSep(speator), mSkipRows(skip_rows), mlabelIdx(labelIdx) {
		//if (QFile::exists(fn)) { 应该在外部检查，就像qfile一样
		//}
		mFile.setFileName(fn);
		mFile.open(QIODevice::ReadOnly);
		size_t i = 0;
		for (; i < mSkipRows; i++) mFile.readLine();
		if (mlabelIdx < 0 || mlabelIdx < mSkipRows) {//小于0或小于被跳过，表示没有列标签，添加默认标签
			//throw std::exception("ccnima");
			auto p = pos();
			header = readLineRaw();
			for (auto i = 0; i < header.size(); i++) {
				header[i] = QString::number(i);
			}
			mFile.seek(p);
		}
		else {
			for (; i < mlabelIdx; i++) mFile.readLine();
			header = readLineRaw();
		}
	}

	CsvReader::~CsvReader() {
	}
	CsvPlot::CsvPlot() {
		zoomer = new QwtPlotZoomer(canvas());
		zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
			Qt::RightButton, Qt::ControlModifier);//左键放大、右键+ctrl恢复原样
		zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
			Qt::RightButton);//左键放大，右键恢复原样
		zoomer->setRubberBandPen(QColor(Qt::black));
		zoomer->setTrackerPen(QColor(Qt::black));
		zoomer->setZoomBase(false);

		panner = new QwtPlotPanner(canvas());
		panner->setMouseButton(Qt::MiddleButton);

		//setAxisAutoScale(true);
		//setAutoFillBackground(true);
		setAxisTitle(QwtAxis::XBottom, "title");
		//QwtDateScaleDraw* draw = new QwtDateScaleDraw(Qt::LocalTime);
		//QwtDateScaleEngine* engine = new QwtDateScaleEngine(Qt::LocalTime);
		draw = new QwtDateScaleDraw(Qt::LocalTime);
		engine = new QwtDateScaleEngine(Qt::LocalTime);
		setAxisScaleDraw(QwtAxis::XBottom, draw);
		setAxisScaleEngine(QwtAxis::XBottom, engine);

		auto curve = new QwtPlotCurve();

		curve->setSamples(xdata, ydata);
		curve->attach(this);
	}

	CsvPlot::~CsvPlot() {
	}
}

/*!
   \brief A scale engine for date/time values

   QwtDateScaleEngine builds scales from a time intervals.
   Together with QwtDateScaleDraw it can be used for
   axes according to date/time values.

   Years, months, weeks, days, hours and minutes are organized
   in steps with non constant intervals. QwtDateScaleEngine
   classifies intervals and aligns the boundaries and tick positions
   according to this classification.

   QwtDateScaleEngine supports representations depending
   on Qt::TimeSpec specifications. The valid range for scales
   is limited by the range of QDateTime, that differs
   between Qt4 and Qt5.

   Datetime values are expected as the number of milliseconds since
   1970-01-01T00:00:00 Universal Coordinated Time - also known
   as "The Epoch", that can be converted to QDateTime using
   QwtDate::toDateTime().

   \sa QwtDate, QwtPlot::setAxisScaleEngine(),
	  QwtAbstractScale::setScaleEngine()
 */